package com.example.batch.reader;

import com.example.batch.dto.IdDto;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.LineCallbackHandler;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.stereotype.Component;

import java.beans.PropertyEditor;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/7/12 09:45
 */
@Component
public class MyFlatFileReader extends FlatFileItemReader {
    public static final String filaPath = "/Users/yuhang/Downloads/ids.txt";

    /**
     * 分隔符文件读取配置
     */
    public MyFlatFileReader() {
        setResource(new PathResource(filaPath));
        setLineMapper(new DefaultLineMapper<IdDto>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{  //一行内容分割默认用','分割
                setNames(new String[]{"id"});
                setComments(new String[]{"==","##"});  //# 为默认的注释符号
                //setLinesToSkip(1);
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper() {{
                setTargetType(IdDto.class);
            }});
        }});
    }

}
